import os
import subprocess
import time
from string import *

import emoji as emoji

'''视频格式转换'''


def cost(file_path, oldcode, newcode):
    count = 1
    for dirpath, dirnames, filenames in os.walk(file_path):
        for filename in filenames:
            try:
                if not filename.__contains__(oldcode) or filename.__contains__("-") or filename.endswith(
                        ")") or filename.endswith("）"):
                    # if not filename.__contains__(oldcode):
                    print("skip filename:" + filename)
                    continue
                inputfilename = dirpath.replace("\\", "/") + '/' + filename
                print(inputfilename)
                outputfilename = str.replace(inputfilename, oldcode, newcode)
                runstr = subprocess.run(
                    'D:/python/ffmpeg-4.3.1-win64-static/bin/ffmpeg.exe -y -i %s -c:v copy -c:a copy  %s' % (
                    inputfilename, outputfilename))
                print("cost success " + str(runstr))
                print("delete file: %s ......" % inputfilename)
                os.remove(inputfilename)
                print("delete file: %s success" % inputfilename)
                print(str(count))
                count += 1
                print("==============================================================")
            except Exception as e:
                print("except" + str(e))
                continue


def rename(file_path):
    for dirpath, dirnames, filenames in os.walk(file_path):
        for filename in filenames:
            try:
                inputfilename = dirpath.replace("\\", "/") + '/' + filename
                filename = emoji.demojize(filename)
                print('filename no emoji:' + filename)
                tables = str.maketrans('', '', "()【】  */" + r"""!"#$%&'()*+，,/:;<=>?@[\]^_`{|}~""" + whitespace)
                filename = str.strip(filename.translate(tables))
                if inputfilename.__eq__(dirpath.replace("\\", "/") + '/' + filename):
                    continue
                if os.path.exists(dirpath.replace("\\", "/") + '/' + filename):
                    tt = str(time.time())
                    laststr = ''
                    if filename.__contains__('.mp4'):
                        laststr = '.mp4'
                    if filename.__contains__('.mov'):
                        laststr = '.mov'
                    if filename.__contains__('.MOV'):
                        laststr = '.MOV'
                    if filename.__contains__('.txt'):
                        laststr = '.txt'
                    if filename.__contains__('.jpg'):
                        laststr = '.jpg'
                    if filename.__contains__('.png'):
                        laststr = '.png'
                filename = filename[0:filename.index('.')] + tt[tt.index('.'):] + laststr
                print('filename + date:' + filename)
                outfilename = dirpath.replace("\\", "/") + '/' + filename
                print('inputfilename:' + inputfilename)
                print('outfilename:' + outfilename)
                os.rename(inputfilename, outfilename)
            except FileExistsError as f:
                print(f)
                continue


# def rename(file_path):
#     for dirpath, dirnames, filenames in os.walk(file_path):
#         for filename in filenames:
#             try:
#                 inputfilename = dirpath.replace("\\", "/") + '/' + filename
#                 if filename.__contains__('.mp4'):
#                     filename = filename[0: filename.find('.mp4')+4]
#                 if filename.__contains__('.txt'):
#                     filename = filename[0: filename.find('.txt')+4]
#                 print('filename:'+filename)
#                 if inputfilename.__eq__(dirpath.replace("\\", "/") + '/' + filename):
#                     continue
#                 outfilename = dirpath.replace("\\", "/") + '/' + filename
#                 print('inputfilename:' + inputfilename)
#                 print('outfilename:' + outfilename)
#                 os.rename(inputfilename, outfilename)
#             except FileExistsError as f:
#                 print(f)
#                 continue


def setlastcode(file_path, lastcode):
    for dirpath, dirnames, filenames in os.walk(file_path):
        for filename in filenames:
            try:
                if filename.__contains__(".flv") or filename.__contains__(
                        ".mp4") or filename.__contains__(".avi") or filename.__contains__(".txt"):
                    continue
                inputfilename = dirpath.replace("\\", "/") + '/' + filename
                outfilename = dirpath.replace("\\", "/") + '/' + filename + lastcode
                os.rename(inputfilename, outfilename)
            except FileExistsError as f:
                print(f)
                continue


def replaceFilename(file_path, st1, st2):
    for dirpath, dirnames, filenames in os.walk(file_path):
        for filename in filenames:
            try:
                if not filename.__contains__(st1):
                    continue
                inputfilename = dirpath.replace("\\", "/") + '/' + filename
                outfilename = dirpath.replace("\\", "/") + '/' + filename.replace(st1, st2)
                os.rename(inputfilename, outfilename)
            except FileExistsError as f:
                print(f)
                continue


if __name__ == '__main__':
    file_path = r"E:\迅雷下载"
    # rename(file_path)  # 文件名删除特殊符号
    # setlastcode(file_path, ".flv")
    # replaceFilename(file_path, ".flv.flv", ".flv")
    cost(file_path, ".TS", ".mp4")
